package com.rt.tmpt.service.impl;

import com.inspiresoftware.lib.dto.geda.assembler.DTOAssembler;
import com.rt.tmpt.bean.A2Company;
import com.rt.tmpt.bean.A2Post;
import com.rt.tmpt.dao.intf.IA2CompanyDao;
import com.rt.tmpt.dao.intf.IA2LowUserTmpDao;
import com.rt.tmpt.dao.intf.IA2PostDao;
import com.rt.tmpt.dao.intf.IA2UserDao;
import com.rt.tmpt.dto.A2PostDto;
import com.rt.tmpt.dto.Result;
import com.rt.tmpt.service.IOperationlogService;
import com.rt.tmpt.service.IOrgService;
import lombok.extern.slf4j.Slf4j;
import org.dozer.Mapper;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Service
@Transactional(propagation = Propagation.REQUIRED)
@Slf4j
public class OrgService implements IOrgService {
    @Resource
    IA2CompanyDao a2CompanyDao;
    @Resource
    IA2UserDao a2UserDao;
    @Resource
    IA2PostDao a2PostDao;
    //@Resource
    IA2LowUserTmpDao a2LowUserTmpDao;
    @Autowired
    private Mapper mapper;

    @Resource
    private IOperationlogService operationlogService;


    @Override
    public Result<A2PostDto> addPost(A2PostDto postDto, long parentPostId, long companyId) {
        Result result = new Result();

        A2Post post = new A2Post().setDefaultValues();
        DTOAssembler dtoAssembler = DTOAssembler.newAssembler(A2PostDto.class);
        dtoAssembler.assembleEntity(postDto, post, null, null);
        post.setDeleteFlag(0);
        //找上级岗位
        A2Post parentPost = null;
        if (parentPostId != 0) {
            parentPost = a2PostDao.findById(parentPostId).get();
        }
        //查所属公司
        A2Company company = a2CompanyDao.findById(companyId).get();
        //添加数据
        post.setA2Company(company);
        post.setA2Post(parentPost);
        a2PostDao.save(post);

        return result.success();
    }

    @Override
    public Result<A2PostDto> queryPostById(long id) {
        Result<A2PostDto> result = new Result();

        A2Post post = a2PostDao.findById(id).get();
        A2PostDto postDto = new A2PostDto();
        DTOAssembler dtoAssembler = DTOAssembler.newAssembler(A2PostDto.class);
        dtoAssembler.assembleDto(postDto, post, null, null);
        result.setObj(postDto);

        return result.success();
    }

    @Override
    public Result<A2PostDto> modifyPostById(A2PostDto postDto) {
        Result<A2PostDto> result = new Result();

        A2Post post = a2PostDao.findById(postDto.getId()).get();
        post.setName(postDto.getName());
        a2PostDao.save(post);

        return result.success();
    }

    @Override
    public Result<A2PostDto> removePostById(long id) {
        Result<A2PostDto> result = new Result();

        A2Post post = a2PostDao.findById(id).get();
        post.setDeleteFlag(1);
        a2PostDao.save(post);

        return result.success();
    }

    @Override
    public Result<A2PostDto> queryPostJsonTreeByCompanyId(long companyId) {
        Result<A2PostDto> result = new Result();

        A2Company company = a2CompanyDao.findById(companyId).get();
        String companyName = company.getName();
/*

		Search search = new Search();
		search.addFilterEqual("a2Company.id",companyId);
		search.addFilterEqual("deleteFlag", 0);
		search.addFilterEmpty("a2Post");
		List<A2Post> postList = null;//????????a2PostDao.search(search);
*/


		/*A2Company a2Company = new A2Company();
		a2Company.setId(companyId);
		A2Post a2Post = new A2Post();
		a2Post.setA2Company(a2Company);
		a2Post.setDeleteFlag(0);
		List<A2Post> postList = a2PostDao.findAll(Example.of(a2Post));*/
        List<A2Post> postList = a2PostDao.findTopA2PostByA2CompanyId(companyId);

        //解析
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", "cId-" + companyId);
        jsonObject.put("companyId", companyId);
        jsonObject.put("text", companyName);
        jsonObject.put("name", companyName);
        jsonObject.put("state", "opened");

        //map
        Map map = new HashMap<>();
        map.put("id", "cId-" + companyId);
        map.put("companyId", companyId);
        map.put("type", 0);
        map.put("text", companyName);
        map.put("name", companyName);
        map.put("state", "opened");

        //递归生成树
        genPostJsonTree(jsonObject, map, postList, companyId);

        JSONArray jsonArray = new JSONArray();
        jsonArray.put(jsonObject);
        String jsonArrayStr = jsonArray.toString();

        List list = new ArrayList<>();
        list.add(map);

        System.out.println("list" + list);
        result.getMap().put("list", list);
        result.getMap().put("jsonArrayStr", jsonArrayStr);
        return result.success();
    }

    /**
     * 递归Post生成树
     */
    private void genPostJsonTree(JSONObject jsonObject, Map map, List<A2Post> postList, long companyId) {
        JSONArray jsonArray = new JSONArray();
        List list = new ArrayList<>();

        jsonObject.put("children", jsonArray);
        map.put("children", list);
        for (A2Post post : postList) {
            long id = post.getId();
            int deleteFlag = post.getDeleteFlag() == null ? 0 : post.getDeleteFlag().intValue();
            if (deleteFlag == 1) {
                continue;
            }
            String name = post.getName();

            //json
            JSONObject jsonObjectSon = new JSONObject();
            jsonObjectSon.put("id", id);
            jsonObjectSon.put("text", name);
            jsonObjectSon.put("name", name);
            //jsonObjectSon.put("state","opened");
            //jsonObjectSon.put("state","closed");
            jsonArray.put(jsonObjectSon);

            //map
            Map mapSon = new HashMap<>();
            mapSon.put("id", id);
            mapSon.put("companyId", companyId);
            mapSon.put("type", 1);
            mapSon.put("text", name);
            mapSon.put("name", name);
            list.add(mapSon);

            List<A2Post> postSonList = post.getA2PostList();
            if (postSonList.size() == 0) {
                continue;
            }
            genPostJsonTree(jsonObjectSon, mapSon, postSonList, companyId);
        }
    }

    private Lock _lock = new ReentrantLock();// 锁对象

    /**
     * 初始化下指定员工的下属员工及岗位内员工
     * userType:员工类型:1,下属员工; 2,同岗位同级员工
     *
     * @param userId
     * @return
     */
    @Override
    public Result initPostUser(long userId) {
		/*try{
			_lock.lock();

			Result result = new Result();

			//先清空用户数据
			clearSonPostUser(userId);

			A2User user = a2UserDao.findById(userId).get();
			A2Post post = user.getA2Post();
			//取下属岗位
			if(post.getDeleteFlag()==1) {return result.success();}
			List<A2Post> allPostSonList = new ArrayList<>();
			getSonPost(post,allPostSonList);
			//下属岗位user
			for (A2Post postLow : allPostSonList) {
				//System.out.println(postLow.getId() + "-" + postLow.getName());
				for(A2User a2User: postLow.getA2UserList()){
					long userIdLowPost = a2User.getId();
					A2LowUserTmp lowUserTmp = new A2LowUserTmp();
					lowUserTmp.setDeleteFlag(0);
					lowUserTmp.setUserId(userId);
					lowUserTmp.setUserType(1);
					lowUserTmp.setLowUserId(userIdLowPost);
					a2LowUserTmpDao.save(lowUserTmp);
				}
			}
			//同岗位user
			List<A2User> userList = post.getA2UserList();
			for(A2User a2User: userList){
				long userIdSamePost = a2User.getId();
				A2LowUserTmp lowUserTmp = new A2LowUserTmp();
				lowUserTmp.setDeleteFlag(0);
				lowUserTmp.setUserId(userId);
				if(userIdSamePost == userId){//本人：设置成本人的下级，这样在查看本人下级时，自己的记录也会出来
					lowUserTmp.setUserType(1);
				}else{
					lowUserTmp.setUserType(2);
				}
				lowUserTmp.setLowUserId(userIdSamePost);
				a2LowUserTmpDao.save(lowUserTmp);
			}
			return result.success();

		}finally {
			_lock.unlock();
		}*/
        return null;

    }

    private void clearSonPostUser(long userId) {
		/*A2LowUserTmp a2LowUserTmp = new A2LowUserTmp();
		a2LowUserTmp.setUserId(userId);
		List<A2LowUserTmp> a2LowUserTmpList = a2LowUserTmpDao.findAll(Example.of(a2LowUserTmp));
		for(A2LowUserTmp lowUserTmp:a2LowUserTmpList){
			a2LowUserTmpDao.delete(lowUserTmp);
		}*/
    }

    /**
     * 取下属岗位
     *
     * @param post
     * @param allPostSonList 存放下级岗位
     * @return
     */
    private void getSonPost(A2Post post, List<A2Post> allPostSonList) {
        List<A2Post> postList = post.getA2PostList();
        for (A2Post postSon : postList) {
            if (post.getDeleteFlag() == 1) {
                continue;
            }
            allPostSonList.add(postSon);
            getSonPost(postSon, allPostSonList);
        }
    }


    /**
     * 取下属员工信息
     *
     * @param userId
     * @return
     */
    @Override
    public Result queryLowPostUsersByUser(long userId) {
        Result result = new Result();
/*

		List<A2UserDto> list = new ArrayList<>();

		A2LowUserTmp a2LowUserTmpParam = new A2LowUserTmp();
		a2LowUserTmpParam.setUserId(userId);
		a2LowUserTmpParam.setUserType(1);
		List<A2LowUserTmp> a2LowUserTmpList = a2LowUserTmpDao.findAll(Example.of(a2LowUserTmpParam));

		for(A2LowUserTmp a2LowUserTmp : a2LowUserTmpList){
			long lowUserId = a2LowUserTmp.getLowUserId();
			if(lowUserId == userId)continue;//下属是自己，不用显示了。下属是自己放入表里，主要是查询时要用
			A2User a2User = a2UserDao.findById(lowUserId).get();
			A2UserDto a2userDto = mapper.map(a2User, A2UserDto.class);
			String a2postName = "";
			if(a2User.getA2Post()!=null){
				a2postName = a2User.getA2Post().getName();
			}
			a2userDto.setA2postName(a2postName);
			list.add(a2userDto);
		}

		result.getMap().put("rows",list);
		result.getMap().put("total",list.size());
*/

        return result.success();
    }


}

